layui.config({
    base : "js/information/"
}).use(['form','layer','jquery','laypage','laydate'],function(){
    var form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        laydate = layui.laydate,
        $ = layui.jquery;

    var pageSize = 10;
    var currPage = 0;
    var flag = $('#flag').val();

    initList();

    function initList(){
        var title = $("#search_input").val().replace(/(^\s*)|(\s*$)/g,"");
        var navType = $("#navType").val();
        var startTime = $("#startTime").val().replace(/(^\s*)|(\s*$)/g,"");
        var endTime = $("#endTime").val().replace(/(^\s*)|(\s*$)/g,"");

        if(startTime) startTime = startTime + " 00:00:00";
        if(endTime) endTime = endTime + " 23:59:59";

        // 页面初始化
        var data = {page:currPage,pageSize:pageSize,title:title,startTime:startTime,endTime:endTime,navType:navType,flag:flag};
        $.ajax({
            type: 'POST',
            url: "/information/getPublishedList",
            data: data,
            dataType: "text",
            success: function(data){
                var data = data.replace(/id\":(\d+),/g,"id\":\"$1\",");
                data = $.parseJSON(data);
                var html = template('informationList', data);
                document.getElementById('informationListTb').innerHTML = html;
                if(currPage >= 0) initPage(data["total"]);
            },
            error: function(){
                console.log("请求失败");
            }
        });
    }

    //查询
    $("#searchBtn").click(function(){
        var title = $("#search_input").val().replace(/(^\s*)|(\s*$)/g,"");
        var navType = $("#navType").val();
        var startTime = $("#startTime").val().replace(/(^\s*)|(\s*$)/g,"");
        var endTime = $("#endTime").val().replace(/(^\s*)|(\s*$)/g,"");

        if(startTime) startTime = startTime + " 00:00:00";
        if(endTime) endTime = endTime + " 23:59:59";

        //if(title != '' && typeof title != undefined){
        var index = layer.msg('查询中,请稍候',{icon: 16,time:false,shade:0.8});

        currPage = 0;
        var data = {page:currPage,pageSize:pageSize,title:title,startTime:startTime,endTime:endTime,navType:navType,flag:flag};
        $.ajax({
            type: 'POST',
            url: "/information/getPublishedList",
            data: data,
            dataType: "text",
            success: function(data){
                var data = data.replace(/id\":(\d+),/g,"id\":\"$1\",");
                data = $.parseJSON(data);
                var html = template('informationList', data);
                document.getElementById('informationListTb').innerHTML = html;
                if(currPage >= 0) initPage(data["total"]);
                layer.close(index);
            },
            error: function(){
                layui.layer.tips('查询失败,请重试', '.layui-layer-setwin .layui-layer-close', {tips: 3});
                layer.close(index);
            }
        });
    });

    form.on("checkbox(choose)",function(data){
        var child = $(data.elem).parents('table').find('tbody input[type="radio"]:not([name="show"])');
        var childChecked = $(data.elem).parents('table').find('tbody input[type="radio"]:not([name="show"]):checked')
        if(childChecked.length == child.length){
            $(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
        }else{
            $(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
        }
        form.render('checkbox');
    })



    // 分页
    function initPage(total){
        laypage({
            cont : "pagination",
            pages : Math.ceil(total/pageSize),
            curr : currPage + 1,
            skip : true,
            jump : function(obj,first){
                currPage = obj.curr - 1;
                if(!first){
                    //window.location.href="/getAllInformationList?page="+obj.curr;
                    initList();
                    $('#informations_list thead input[type="checkbox"]').prop("checked",false);
                    form.render();
                }else{
                    $('#informations_list thead input[type="checkbox"]').prop("checked",false);
                    form.render();
                }
            }
        })
    }

    // 资讯选择
    $("#actors").click(function(){
        var index = layui.layer.open({
            title : "演员选择",
            type : 2,
            content : "../../page/person/actorOpenList.html",
            shadeClose: true,
            closeBtn: 1,
            maxmin: true, //开启最大化最小化按钮
            area: ['860px', '560px'],
            btn: ['确认'],
            yes: function(index, layero){
                var $checkbox = $("#actors_list tbody input[type=\"checkbox\"][name=\"checked\"]", layero.find("iframe")[0].contentWindow.document);
                var $checked = $("#actors_list tbody input[type=\"checkbox\"][name=\"checked\"]:checked", layero.find("iframe")[0].contentWindow.document);

                var objs = [];

                var obj = {};
                for(var j=0;j<$checked.length;j++){
                    obj = $($checked[j]).closest("td").next("td");
                    actorsName.push(obj.text());
                    actorsId.push(obj.attr("actorId"));
                }
                $("#actors").val(actorsName.join(","));
                $("#actorId").val(actorsId.join(","));

                layui.layer.closeAll();
            }
            // success : function(layer, index){
            //     setTimeout(function(){
            //         layui.layer.tips('点击此处返回剧集列表', '.layui-layer-setwin .layui-layer-close', {
            //             tips: 3
            //         });
            //     },500)
            // }
        })
    })
})
